package com.sims.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MySQLConnBean {
	private Connection conn = null;
	private Statement stmt = null;
	ResultSet rs = null;

	// 构造函数
	public MySQLConnBean() {
	}

	public void OpenConn() throws Exception {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			String url = "jdbc:mysql://118.25.39.42:3306/sims";
			String user = "root";
			String password = "pass4321?><M";
			conn = DriverManager.getConnection(url, user, password);
		} catch (SQLException e) {
			System.err.println("Data.executeQuery: " + e.getMessage());
		}
	}

	// 执行查询类的SQL语句，有返回集
	public ResultSet executeQuery(String sql) {
		rs = null;
		try {
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
					ResultSet.CONCUR_UPDATABLE);
			rs = stmt.executeQuery(sql);
		} catch (SQLException e) {
			System.err.println("Data.executeQuery: " + e.getMessage());
		}
		return rs;
	}

	// 关闭对象
	public void closeStmt() {
		try {
			if (stmt != null) {
				stmt.close();
			}
		} catch (SQLException e) {
			System.err.println("Date.executeQuery: " + e.getMessage());
		}
	}

	public void closeConn() {
		try {
			if (conn != null) {
				conn.close();
			}
		} catch (SQLException e) {
			System.err.println("Data.executeQuery: " + e.getMessage());
		}
	}
}
